package com.yaya.generator.engine;

import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.yaya.generator.config.GlobalConfig;
import com.yaya.generator.config.PackageConfig;

import java.io.File;
import java.util.Map;

/**
 * 模板引擎
 * 
 * @author m
 *
 *
 */
public class FreemarkerTemplateEngine extends com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine {

  /**
   * 自定义文件输出配置
   * 
   * @param customFile 自定义文件信息
   * @param tableInfo  表信息
   * @param objectMap  对象信息
   */
  @Override
  protected void outputCustomFile(Map<String, String> customFile, TableInfo tableInfo, Map<String, Object> objectMap) {
    customFile.forEach((fileType, ftlPath) -> {
      switch (fileType) {
        // 传输对象
        case "dto":
          String dtoPath = GlobalConfig.OUTPUT_DIR + "/yiyan-service/src/main/java/";
          dtoPath += PackageConfig.PARENT.replaceAll("\\.", "/");
          dtoPath += PackageConfig.MODULE_NAME.equals("") ? "" : "/" + PackageConfig.MODULE_NAME;
          dtoPath += "/dtomain/" + tableInfo.getEntityName() + "Dto.java";
          outputFile(new File(dtoPath), objectMap, ftlPath);
          break;
        // 管理端接口文件
        case "admin-api":
          String adminApiPath = GlobalConfig.OUTPUT_DIR + "/yiyan-admin/src/http/api/";
//          adminApiPath += DataSourceConfig.SCHEMA + "/";
//          adminApiPath += PackageConfig.MODULE_NAME.equals("") ? "" : PackageConfig.MODULE_NAME + "/";
          adminApiPath += tableInfo.getName().replaceAll("_", "-") + ".ts";
          outputFile(new File(adminApiPath), objectMap, ftlPath);
          break;
        // 管理端界面文件
        case "admin-view":
          String adminViewPath = GlobalConfig.OUTPUT_DIR + "/yiyan-admin/src/views/";
//          adminViewPath += DataSourceConfig.SCHEMA + "/";
//          adminViewPath += PackageConfig.MODULE_NAME.equals("") ? "" : PackageConfig.MODULE_NAME + "/";
          adminViewPath += tableInfo.getName().replaceAll("_", "-") + ".vue";
          outputFile(new File(adminViewPath), objectMap, ftlPath);
          break;
        // 其他文件
        default:
          String otherPath = GlobalConfig.OUTPUT_DIR + "/other/" + tableInfo.getEntityName() + "." + fileType;
          outputFile(new File(otherPath), objectMap, ftlPath);
          break;
      }
    });
  }

}
